package nsq

import (
	sqlc "bp/db/sqlc"
	"context"
)

// UpsertNsqSub 新建/更新nsq_sub接收消息
func (s *NsqEntityServiceImp) UpsertNsqSub(ctx context.Context, q *sqlc.Queries, topic, sn string, messageBytes []byte) (nsqSubPtr *sqlc.NsqSub, err error) {
	nsqSub, err := q.UpsertNsqSub(ctx, sqlc.UpsertNsqSubParams{
		Topic:   topic,
		Sn:      sn,
		Message: messageBytes,
	})
	if err != nil {
		return
	}
	nsqSubPtr = &nsqSub
	return
}

// MarkNsqSubHandleSuccess 标记nsq接收消息已处理
func (s *NsqEntityServiceImp) MarkNsqSubHandleSuccess(ctx context.Context, q *sqlc.Queries, id int64) (err error) {
	err = q.MarkNsqSubHandleSuccess(ctx, id)
	if err != nil {
		return
	}
	return
}

// UpdateNsqSubError 更新nsq接收消息错误
func (s *NsqEntityServiceImp) UpdateNsqSubError(ctx context.Context, q *sqlc.Queries, id int64, errMsg string) (err error) {
	err = q.UpdateNsqSubError(ctx, sqlc.UpdateNsqSubErrorParams{
		ID: id,
		ErrMsg: sqlc.NSFromString(errMsg),
	})
	if err != nil {
		return
	}
	return
}